$(document).ready(function () {

    var loginVue = new Vue({
        el: '#mid-right',
        data: {
            exParams: {
                showError: false,
                errMsg: ''
            },
            editFormValues: {
                username: $.cookie('login_username') ? $.cookie('login_username') : '',
                password: $.cookie('login_password') ? $.cookie('login_password') : '',
                remember_pwd: $.cookie('login_remember_pwd') === '1'
            }
        },
        methods: {

            username_keyup: function () {
                Vue.set(loginVue.exParams, 'errMsg', '');
                Vue.set(loginVue.exParams, 'showError', false);
            },

            password_keyup: function () {
                Vue.set(loginVue.exParams, 'errMsg', '');
                Vue.set(loginVue.exParams, 'showError', false);
            },

            loginAction: function (event) {

                if (!loginVue.editFormValues.username) {
                    $('#mid-right').shake(2, 10, 400);
                    Vue.set(loginVue.exParams, 'errMsg', '请输入用户名');
                    Vue.set(loginVue.exParams, 'showError', true);
                    return;
                }

                if (!loginVue.editFormValues.password) {
                    $('#mid-right').shake(2, 10, 400);
                    Vue.set(loginVue.exParams, 'errMsg', '请输入密码');
                    Vue.set(loginVue.exParams, 'showError', true);
                    return;
                }

                swal({
                    title: '正在登录',
                    text: '请您耐心等候',
                    allowOutsideClick: false,
                    onOpen: () => {
                        swal.showLoading();
                    }
                });

                app.callAjaxPost('../../../../api/LoginController/login', {
                    username: loginVue.editFormValues.username,
                    password: loginVue.editFormValues.password
                }, function (result) {
                    swal.close();
                    if (result.success) {
                        if (loginVue.editFormValues.remember_pwd) {
                            $.cookie('login_username', loginVue.editFormValues.username, {expires: 7, path: '/'});
                            $.cookie('login_password', loginVue.editFormValues.password, {expires: 7, path: '/'});
                            $.cookie('login_remember_pwd', loginVue.editFormValues.remember_pwd ? '1' : '0', {
                                expires: 7,
                                path: '/'
                            });
                        } else {
                            $.cookie('login_username', '', {expires: -1, path: '/'});
                            $.cookie('login_password', '', {expires: -1, path: '/'});
                            $.cookie('login_remember_pwd', '', {
                                expires: -1,
                                path: '/'
                            });
                        }
                        window.location.href = '../../index/index.html';
                    } else {
                        $('#mid-right').shake(2, 10, 400);
                        Vue.set(loginVue.exParams, 'errMsg', result.message);
                        Vue.set(loginVue.exParams, 'showError', true);
                    }
                })
            },

            checkbox_click: function (event) {
                if (event.target.checked) {
                    Vue.set(loginVue.editFormValues, 'remember_pwd', true);
                } else {
                    Vue.set(loginVue.editFormValues, 'remember_pwd', false);
                }

            }
        }
    })


});